<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$config = array(
		//
		'cname' => 'item',
		
		//
		'class_file' => 'module/vote/vote.survey.class.php',
		'class_name' => 'VoteSurveyBase',
		'class_param' => array('_table_status_usable' => false),
		
		//
		'inject' => array(
				'categories_options' => array(
									'type' => 'cm',
									'class_file' => 'module/vote/vote.category.class.php', 
									'class_name' => 'VoteCategoryBase',
									'method' => 'getCategoriesTreeFormat',
									'params' => array('', 0, false),
										),
					),
		
		//
		'tpl_type' => 'admin',
		'tpl_file' => 'pagelayout/pagelayout_admin_vote_survey.tpl',
		
		//
		'validate_file' => '',
		'validate_class' => 'CommonValidate',
		'validate_fields' => array('field' => 'name', 'valid' => 'null'),
			);

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.control.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.content.control.class.php');

//
class _ControlItemContentBase extends ControlItemContentBase {
	
	//constructor
	function _ControlItemContentBase($config = '') {
		parent::ControlItemContentBase($config);
		
		//
		if( $this->feObj->action == 'updatelink' ) {
			$this->actionHandler = 'doUpdateLinkActionHandler';
		} elseif( $this->feObj->action == 'createcategory' ) {
			$this->actionHandler = 'doCreateCategoryActionHandler';
		}
		//
		if( $this->feObj->operation == 'detail' ) {
			$this->displayHandler = 'doDetailDisplayHandler';
		} elseif( $this->feObj->operation == 'link' ) {
			$this->displayHandler = 'doLinkDisplayHandler';
		} elseif( $this->feObj->operation == 'call' ) {
			$this->displayHandler = 'doCallDisplayHandler';
		}
	}
	
	//methods
	function doActionHandler() {
		echo 'doActionHandler';
		exit;
	}
	
	function doUpdateLinkActionHandler($obj) {
		if( is_array($_POST['field_link']) ) {
			foreach($_POST['field_link'] as $k => $v) {
				$id = (substr($k, 0, 1) == 'a') ? substr($k, 1) : $k;
				if( $id > 0 ) {
					$this->modelObj->_generate_item_link($id, '', $v);
					//
					$item = $this->modelObj->getItem($id, 'id');
					if( $item['id'] > 0 && $item['app1'] == '1' && $item['app2'] > 0 ) {
						$this->_update_item_menu_link($item['app2'], $item['link']);
					} elseif( $item['id'] > 0 && $item['app3'] == '2' && $item['app4'] > 0 ) {
						$this->_update_item_category_link($item['app4'], $item['link']);
					}
				}
			}
		}
		
		_dms(':admin:update_link_success');
		$this->_redirect('', '', 's');
	}
	
	function _update_item_menu_link($id, $link) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/menu/menu.class.php');
		$mObj = new MenuBase();
		$mObj->format = true;
		
		$mObj->_generate_item_link($id, '', $link);
	}
	
	function _update_item_category_link($id, $link) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.category.class.php');
		$mcObj = new ContentCategoryBase();
		$mcObj->format = true;
		
		$mcObj->_generate_category_link($id, '', $link);
	}
	
	function doCreateCategoryActionHandler($obj) {
		$id = $this->feObj->_post['id'];
		$item = $this->modelObj->getItem($id, 'id', -1);
		
		//
		if( $item['id'] == 0 ) {
			_dms(':admin:item_not_exist');
			$this->_redirect('', '', 's');
		}
		
		//
		$r = $this->modelObj->createItemCategory($item);
		if( $r == 2 ) {
			_dms(':admin:create_category_exist');
			$this->_redirect('', '', 's');
		} elseif( $r == 1 ) {
			_dms(':admin:create_category_success');
			$this->_redirect('', '', 's');
		}
	}
	
	//
	function doDisplayHandler() {
		echo 'doDisplayHandler';
		exit;
	}
	
	function doDetailDisplayHandler($obj) {
		$id = $this->feObj->_request['id'];
		if( $id > 0 ) {
			$item = $this->modelObj->getItem($id, 'id', -1);
			
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/vote/vote.class.php');
			$mObj = new VoteBase();
			$mObj->format = true;
			$vote_items = $mObj->getItems(array('sid' => $item['id']), 0, false);
			
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/vote/vote.history.class.php');
			$mObj = new VoteHistoryBase();
			$mObj->format = true;
			$history_items = $mObj->getItems(array('sid' => $item['id']), 0, false);
			
			$this->feObj->content_wrap['mainpart'] = 'detail';
			$this->feObj->content_wrap['item'] = $item;
			$this->feObj->content_wrap['vote_items'] = $vote_items;
			$this->feObj->content_wrap['history_items'] = $history_items;
		} else {
			$this->_redirect('', array('op'), '');
		}
	}
	
	function doLinkDisplayHandler($obj) {
		$navigation = array();
		$limit = true;
		if( $this->feObj->_request['cid'] > 0 && $this->feObj->_request['nolimit'] == '1' ) {
			$limit = false;
		}
		$check = -1;
		if( $this->feObj->_request['check'] == '1' ) {
			$check = 0;
		}
		
		//
		if( $this->feObj->_request['cid'] > 0 ) {
			$filter = $this->_get_items_filter();
			$items = $this->modelObj->getItems($filter, $check, $limit);
		} else {
			$filter = $this->_get_items_filter();
			$limit_length = $this->feObj->limit_length;
			$limit_start = ($this->feObj->limit_page - 1) * $limit_length;
			$limit_total = $this->modelObj->getItemsTotal($filter, -1);
			//
			$this->modelObj->setQueryLimitStart($limit_start);
			$this->modelObj->setQueryLimitLength($limit_length);
			$items = $this->modelObj->getItems($filter, $check, $limit);
			//
			$parameters = util_get_all_get_params(array('page'));
			$link_array = array(
						'page' => '',
						'parameters' => $parameters,
							);
			$navigation = util_common_navigation($link_array, $limit_total, $limit_start, $limit_length);
		}
		
		//
		$this->feObj->content_wrap['mainpart'] = 'link';
		$this->feObj->content_wrap['items'] = $items;
		$this->feObj->content_wrap['navigation'] = $navigation;
	}
	
	function doCallDisplayHandler($obj) {
		$navigation = array();
		$limit = true;
		if( $this->feObj->_request['cid'] > 0 && $this->feObj->_request['nolimit'] == '1' ) {
			$limit = false;
		}
		$check = -1;
		if( $this->feObj->_request['check'] == '1' ) {
			$check = 0;
		}
		
		//
		if( $this->feObj->_request['cid'] > 0 ) {
			$filter = $this->_get_items_filter();
			$items = $this->modelObj->getItems($filter, $check, $limit);
		} else {
			$filter = $this->_get_items_filter();
			$limit_length = $this->feObj->limit_length;
			$limit_start = ($this->feObj->limit_page - 1) * $limit_length;
			$limit_total = $this->modelObj->getItemsTotal($filter, -1);
			//
			$this->modelObj->setQueryLimitStart($limit_start);
			$this->modelObj->setQueryLimitLength($limit_length);
			$items = $this->modelObj->getItems($filter, $check, $limit);
			//
			$parameters = util_get_all_get_params(array('page'));
			$link_array = array(
						'page' => '',
						'parameters' => $parameters,
							);
			$navigation = util_common_navigation($link_array, $limit_total, $limit_start, $limit_length);
		}
		
		//
		$this->feObj->content_wrap['mainpart'] = 'call';
		$this->feObj->content_wrap['items'] = $items;
		$this->feObj->content_wrap['navigation'] = $navigation;
	}
	
	//
	function _doAfter() {
		
	}
}

//
$cntlObj = new _ControlItemContentBase($config);
$cntlObj->process();
//
?>